package base;

import freemarker.template.*;
import java.util.*;
import java.io.*;




public class AutoCode {

    public static void execute(String[] args) throws Exception {
    	if(args.length < 3){
    		System.out.println("Usage: jp.bat -xml -template -outfile -xml -para");
    		System.exit(0);
    	}
    	String templateDir = GetFtlPath();//主目录设置 bin的上层目录

    	System.out.println("运行目录："+getRunPath()+"\n");
    	
    	String dataModel =GetXmlPath()+args[0];  	
    	System.out.println("数据字典："+dataModel+"\n");
    	
    	String template = args[1];//基于ftl主目录的相对路径
    	System.out.println("模板文件："+template+"\n");
    	
    	String _at=args[2];
    	String outfile;
    	if(_at.indexOf('@')==0)
    	{
    		outfile=GetFtlPath()+_at.substring(1);//输出文件名
    		System.out.println("输出文件：【"+GetFtlPath()+"】"+_at.substring(1)+"\n");	    	}
    	else
    	{
    		outfile=GetOutPath()+args[2];//输出文件名
    		System.out.println("输出文件：【"+GetOutPath()+"】"+args[2]+"\n");	
    	}
    	
    	
		String dataModel2 ="";
		String para1 ="";
		if( args.length>3)
		{
	    	dataModel2 = (args[3]);
	    	if(dataModel2.equals("null.xml")||dataModel2.equals("null"))
	    		dataModel2="";
	    	else
	    		dataModel2 =GetXmlPath()+args[3];
		}
		if( args.length>4)
			para1 = args[4];
		System.out.println("附加数据字典："+dataModel2+"\n");
		System.out.println("参数1："+para1+"\n");
        /* Adjust the configuration */
        Configuration cfg =new Configuration();
       
        cfg.setDirectoryForTemplateLoading(          new File(templateDir));
        
        cfg.setEncoding(Locale.getDefault(), "UTF-8");
        /* Create a template */
        Template temp = cfg.getTemplate(template);

        /* Create a data model */
        HashMap<Object,Object> root = new HashMap<Object,Object>();
		root.put(
        "doc",
        freemarker.ext.dom.NodeModel.parse(new File(dataModel)));  
		
	 
		if(dataModel2 != "" && !dataModel2.equals(""))
		{
			root.put(
			"sec",
			freemarker.ext.dom.NodeModel.parse(new File(dataModel2)));
			
		}
	 //	System.out.println("debuG："+dataModel3+"\n");
	 	String encode="UTF-8";
		if(para1 != "" && !para1.equals(""))
		{
			String strs[];
			if( para1.indexOf(";")>=0 )
				 strs=para1.split(";");
			else
				 strs=para1.split("#");
			String strss[];
			String var,val;
			for(String str:strs)
			{
				strss=str.split(":");
				var=strss[0];
				if(strss.length>1)
					val=strss[1];
				else
					val="null";
				if(var.equalsIgnoreCase("encode"))
					encode=val;
				else
					
					root.put(var,	new CmdParaValue(val));		
			}
			
 		}
        FileOutputStream fout = new FileOutputStream(outfile);
        /* Merge data model with template */
        Writer out = new OutputStreamWriter(fout,encode);
        //Writer out = new OutputStreamWriter(System.out);
        temp.process(root, out);
        out.flush();
        fout.close();
        System.out.println("执行完毕！" +   "\n");
    }
    static public String  getFilePath(String fname)
	{
    	return fname;
    
	}
    static public String  getRunPath()
	{
  
    	
    	String tmp="";
    	String   userDir   =   	AutoCode.class.getResource("/").getPath() ;
    	String a2[]=userDir.split("/");
		int len2=a2.length;
		for(int i=0;i<len2-1;i++)
		{
			
			tmp+=a2[i];
			tmp+="/";
		}
		return tmp+"src/";//返回 ormap为 运行目录，所有路径都是基于此的相对路径

		
		
	
	}
   
   
    static public String  GetXmlPath()
    {
    	return getRunPath()+"data/";
    }
    static public String  GetFtlPath()
    {
    	return getRunPath()+"ftl/";
    }
    static public String  GetOutPath()
    {
    	String tmp="";
    	String   userDir   =   	AutoCode.class.getResource("/").getPath() ;
    	String a2[]=userDir.split("/");
		int len2=a2.length;
		for(int i=0;i<len2-3;i++)
		{
			
			tmp+=a2[i];
			tmp+="/";
		}
		return tmp;//返回 ormap为 运行目录，所有路径都是基于此的相对路径

    }

}   
  
   